# Using a combination of sass_compiler and haml_compiler, tailored for this project
# 
# @require: sass gem, haml gem

require "fileutils" # required for exec_mov

# To-Do: Convert a similar script to main Rosarla module
# Make Rosarla a gem

module Build
	
	def self.success(file, output_file)
		puts "'#{file}' successfully compiled to '#{output_file}'."
	end
	
	def self.exec(files, extStart, extEnd, compiler, upDirectory)
		extra = (compiler == "sass") ? " --style compressed" : ""
		(files).each do |file|
			output_code = %x(#{compiler} #{file}#{extra})
			output_file = upDirectory + file.gsub(extStart, extEnd).gsub("./assetsBuild/", "").gsub("styles/", "")
			output_target = open(output_file, "w")
			output_target.write(output_code)
			output_target.close
			self.success(file, output_file)
		end
	end
	
	def self.exec_mov(files, compiler, upDirectory)
		(files).each do |file|
			get_this = %x(#{compiler} #{file})
			new_file = file.gsub(".ts", ".js")
			output_file = upDirectory + new_file.gsub("./assetsBuild/", "").gsub("scripts/", "")
			
			FileUtils.mv("#{new_file}", "#{output_file}")
			self.success(file, output_file)
		end
	end
	
	def self.sass(files)
		self.exec(files, ".sass", ".css", "sass", "./assets/")
	end
	
	def self.haml(files)
		self.exec(files, ".haml", ".htm", "haml", "")
	end
	
	def self.typescript(files)
		self.exec_mov(files, "tsc", "./assets/")
	end

end

Build.sass(["./assetsBuild/styles/main.sass"])
Build.haml(["./assetsBuild/default.haml"])
Build.typescript(["./assetsBuild/scripts/main.ts"])
